﻿Imports System.IO
Public Class frmNewReceiveItem
    Dim Strsql As String
    Dim _mysql As New ClsSQLhelper
    Dim On_load As Boolean = True
    Public Primary_Key As Integer = -1
    Public New_Rows As Integer = 1
    Dim DT_Section As New DataTable
    Dim DT_Category As New DataTable

#Region "Function"
    Sub Load_Section()
        Try
            Strsql = "Select section_id,section_name,note,active,readonly from section where active =1 "

            DT_Section = _mysql.GetMYSQLDataTable(Strsql, "section")
            With CboSection
                .DataSource = DT_Section
                .DisplayMember = "section_name"
                .ValueMember = "section_id"
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Section"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql

                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Category()
        Try
            Strsql = "Select category_id,category_name,note,active,readonly,sectionid from category where active =1 and sectionid = " & Me.CboSection.SelectedValue

            DT_Category = _mysql.GetMYSQLDataTable(Strsql, "category")
            With CboCategory
                .DataSource = DT_Category
                .DisplayMember = "category_name"
                .ValueMember = "category_id"
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Category"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql

                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Copy_Image2Server()
        Dim StrFilename As String = ""
        Try

            StrFilename = My.Settings.ImgSharePart & "\" & Now.Year.ToString & Now.Month.ToString & Now.Day.ToString & Now.Hour & Now.Minute & Now.Second & ".jpg"

            File.Copy(Me.TxtImagepart.Text, StrFilename, True)
            Dim bm As New Bitmap(Me.TxtImagepart.Text)
            PictureEdit1.Image = bm
            Me.TxtImagepart.Text = StrFilename
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Copy_Image2Server"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = "StrFilename = " & StrFilename
                .ShowDialog()
            End With
        End Try

    End Sub
    Sub Update_Price()
        Me.TxtPriceIncludeVat.EditValue = Me.TxtPrice.Value + ((Me.TxtPrice.Value * Me.TxtVat.Value) / 100)
        Me.TxtTotalprice.EditValue = Me.TxtPriceIncludeVat.EditValue * Me.TxtQty.EditValue

    End Sub
    Sub Insert_Data()
        Try

            If MsgBox("คุณต้องการเพิ่มข้อมูลใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการเพิ่มข้อมูล") = MsgBoxResult.No Then Exit Sub
            If New_Rows = -1 Then
                New_Rows = 1
            Else
                New_Rows = New_Rows + 1
            End If

            Strsql = "Insert into receive_detail (productid,serialno,band,model,priceincludevat,drawqty,receiveqty,totalpriceprice,staff,remarks,warantry,vat,nonvat,priceperunit,nonprice,purchase_date,nonbuydate,mdf_date,nonmfd,imagepart) "
            Strsql = Strsql & " values('" & Me.CboItemType.Text & "','" & Me.TxtSerialno.Text & "','" & Me.TxtBand.Text & "','" & Me.TxtModel.Text & "'"
            Strsql = Strsql & " ," & Me.TxtPriceIncludeVat.Value & "," & Me.TxtQty.Value & "," & Me.TxtReceiveQty.Value & "," & Me.TxtTotalprice.Value & ",'" & Me.TxtStaff.Text
            Strsql = Strsql & "','" & Me.TxtRemarks.Text & "'," & Me.TxtWarantry.Value

            'Check nonvat
            If ChkNonePrice.Checked = True Then
                Strsql = Strsql & ", 0,'Y'"
            Else
                Strsql = Strsql & "," & Me.TxtVat.Value & " ,'N'"
            End If

            'Check nonprice
            If ChkNonePrice.Checked = True Then
                Strsql = Strsql & ",  0,  'Y'"
            Else
                Strsql = Strsql & ", " & Me.TxtPrice.Text & ", 'N'"
            End If

            'Check nonbuydate 
            If ChkNoneBuyDate.Checked = True Then
                Strsql = Strsql & ",  null,  'Y'"
            Else
                Strsql = Strsql & ",  '" & Pn_Framework.MysqlDateTimeFormat(Me.DtpPHDate.DateTime, False) & "',  'N'"
            End If

            'Check nonmfd
            If ChkNonMFD.Checked = True Then
                Strsql = Strsql & ",mdf_date= null,nonmfd = 'Y'"
            Else
                Strsql = Strsql & ",mdf_date='" & Pn_Framework.MysqlDateTimeFormat(Me.DtpMFD.DateTime, False) & "',nonmfd = 'N'"
            End If


            If Me.TxtImagepart.Text = "" Then
                Strsql = Strsql & ",''"
            Else
                Copy_Image2Server()
                Strsql = Strsql & ",'" & Replace(Me.TxtImagepart.Text, "\", "\\") & "'"
            End If



            Strsql = Strsql & ")"

            If _mysql.MySQLExecute(Strsql) > 0 Then
                MsgBox("เพิ่มข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการบันทึกข้อมูล")
                Me.DialogResult = Windows.Forms.DialogResult.OK
            Else
                MsgBox("ไม่สามารถเพิ่มข้อมูลได้", MsgBoxStyle.Information, "ผลการบันทึกข้อมูล")
                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "BtnSave_Click"
                    .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง SQL"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()
                End With

            End If

        Catch ex As Exception

            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnSave_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_ItemType()
        Try
            Strsql = "Select * from itemtype"
            Dim DT_ItemType As New DataTable
            DT_ItemType = _mysql.GetMYSQLDataTable(Strsql, "itemname")
            With CboItemType

                .DataSource = DT_ItemType
                .DisplayMember = "itemname"
                .ValueMember = "itemnumber"

            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
#End Region

    Private Sub frmNewReceiveItem_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If Me.DialogResult <> Windows.Forms.DialogResult.OK Then
            If MsgBox("คุณต้องการออกโดยไม่บันทึกใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.No Then
                Me.DialogResult = Windows.Forms.DialogResult.Cancel
                e.Cancel = True
            End If
        End If
    End Sub
    Private Sub frmNewReceiveItem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.TxtStaff.Text = frmNewPRForm.TxtStaff.Text

        Load_Section()
        Load_Category()
        Load_ItemType()

        Me.On_load = False
    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Insert_Data()
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Dispose()
    End Sub

    Private Sub TxtQty_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Update_Price()
        Me.TxtReceiveQty.Text = TxtQty.Text
    End Sub

    Private Sub TxtPriceIncludeVat_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.TxtTotalprice.Value = Me.TxtPriceIncludeVat.Value * Me.TxtQty.Value
        Me.TxtReceiveQty.Value = Me.TxtQty.Value
    End Sub

    Private Sub TxtPrice_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Update_Price()
    End Sub

    Private Sub BtnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            With Me.OpenFileDialog1
                .Title = "เลือกรูปภาพ"
                .FileName = ""
                .Filter = "Jpeg Image|*.jpg"
                If .ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub

                Me.TxtImagepart.Text = .FileName
                Dim bm As New Bitmap(Me.TxtImagepart.Text)
                PictureEdit1.Image = bm

            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "PicLoadImg_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub ChkNonePrice_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If On_load = True Then Exit Sub
        Select Case Me.TxtPrice.Enabled
            Case True
                Me.TxtPrice.Enabled = False
            Case False
                Me.TxtPrice.Enabled = True

        End Select
    End Sub

    Private Sub ChkNonVat_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If On_load = True Then Exit Sub
        Select Case Me.TxtVat.Enabled
            Case True
                Me.TxtVat.Enabled = False
                Me.TxtVat.EditValue = 0
            Case False
                Me.TxtVat.Enabled = True
                Me.TxtVat.EditValue = 7

        End Select
    End Sub

    Private Sub ChkNoneBuyDate_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If On_load = True Then Exit Sub
        Select Case Me.DtpPHDate.Enabled
            Case True
                Me.DtpPHDate.Enabled = False
            Case False
                Me.DtpPHDate.Enabled = True

        End Select
    End Sub

    Private Sub ChkNonMFD_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If On_load = True Then Exit Sub
        Select Case Me.DtpMFD.Enabled
            Case True
                Me.DtpMFD.Enabled = False
            Case False
                Me.DtpMFD.Enabled = True

        End Select
    End Sub

    Private Sub TxtVat_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Update_Price()
    End Sub

    Private Sub CboItemType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            Strsql = "Select itemimage from itemtype where itemnumber = " & CboItemType.SelectedValue
            Dim MyImgFile As String = _mysql.MySQLExecuteScalar(Strsql)
            If MyImgFile <> "" Then
                Me.TxtImagepart.Text = MyImgFile
                Dim bm As New Bitmap(Me.TxtImagepart.Text)
                PictureEdit1.Image = bm
            End If


        Catch ex As Exception

        End Try
    End Sub

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
End Class